<template>
  <div class="login_container">
    登录
    <div class="login_box">
      <!--头部区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="aa" />
      </div>

      <!--表单区域 -->

      <el-form ref="form" :rules="rules" :model="loginForm" label-width="0px" class="formlogin">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      //表单数据绑定
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //表单重置
    resetForm() {
      console.log(this)
      this.$refs.form.resetFields()
    },
    //登录验证
    login() {
      //this.$http.post('login',this.loginForm)
      this.$refs.form.validate(valid => {
        console.log(this.loginForm)

        if (!valid) return
        const res = this.$http
          .post('login', this.loginForm)
          .catch(function(error) {
            // 请求失败处理
            //console.log("aaaa"+error);
            this.$message('ahfadf')
          })
        // if (res){
        //     this.$message.error("ahfadf");
        // }
        //if(res.meta.status !==200) return console.log('登录失败');
        //存储登录态到sessions

        window.sessionStorage.setItem('token', 'rfadfadf')
        //跳转到主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang = "less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.formlogin {
  position: absolute;
  bottom: 0; /*底部对齐 */
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>

